package com.ningmengs789.live.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.R;
import com.ningmengs789.live.http.JsonBuilder;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.net.network.http.HttpException;
import com.ningmengs789.live.net.response.OssImageResponse;
import com.ningmengs789.live.net.response.OssVideoResponse;
import com.ningmengs789.live.net.response.ReasonListResponse;
import com.ningmengs789.live.net.utils.NToast;
import com.ningmengs789.live.net.utils.json.JsonMananger;
import com.ningmengs789.live.oss.CosXmlUtils;
import com.ningmengs789.live.ui.adapter.ReportAdapter;
import com.ningmengs789.live.utils.UserInfoUtil;
import com.bumptech.glide.Glide;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;
import com.yuyh.library.imgsel.config.ISListConfig;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportActivity extends BaseActivity implements View.OnClickListener, CosXmlUtils.OSSResultListener {

    @BindView(R.id.tv_title_center)
    TextView tv_title_center;

    @BindView(R.id.shimmer_recycler_view)
    RecyclerView shimmerRecycler;

    @BindView(R.id.report_btn)
    Button mReportBtn;

    @BindView(R.id.report_content_et)
    EditText reportContentET;

    @BindView(R.id.report_iv)
    ImageView reportIV;

    private CosXmlUtils uploadOssUtils;
    private String mUserid;
    private String mBeUserID;

    private ReportAdapter reportAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        ButterKnife.bind(this);
        setHeadLayout();

        mUserid = UserInfoUtil.getMiTencentId();
        mBeUserID = getIntent().getStringExtra("BE_USER_ID");

        initView();

        uploadOssUtils = new CosXmlUtils(this);
        uploadOssUtils.setOssResultListener(this);

        ISNav.getInstance().init(new ImageLoader() {
            @Override
            public void displayImage(Context context, String path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });

        getReasonList();
    }

    private void initView() {
        tv_title_center.setVisibility(View.VISIBLE);
        tv_title_center.setText(getString(R.string.dialog_community_option_jubao));

        shimmerRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
        reportAdapter = new ReportAdapter(mContext);
        shimmerRecycler.setAdapter(reportAdapter);

        mReportBtn.setOnClickListener(this);
        reportIV.setOnClickListener(this);

        reportAdapter.setOnItemClickListener(new ReportAdapter.MyItemClickListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked, int position) {
                if (isChecked){
                    hobbies.add(response.getList().get(position).getReason_id());
                }else {
                    hobbies.remove(response.getList().get(position).getReason_id());
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.report_btn:
                if (TextUtils.isEmpty(localUri)) {
                    addInform();
                }else {
                    //（1:个人相册 2:个人视频 3:个人语音介绍 4:达人认证 5:实名认证 6:个人头像 11:动态图片 12:动态视频 21:直播间封面）
                    uploadOssUtils.uploadData(localUri, "image", 11, mUserid, null);
                }

                break;
            case R.id.report_iv:
                tickPhoto();
                break;
        }
    }

    private ArrayList<String> hobbies = new ArrayList<String>();

    private void tickPhoto() {
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.TRANSPARENT)
                // “确定”按钮文字颜色
                .btnTextColor(Color.WHITE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.parseColor("#ffd100"))
                // 返回图标ResId
                .backResId(R.drawable.head_back)
                // 标题
                .title(getString(R.string.per_1))
                // 标题文字颜色
                .titleColor(Color.WHITE)
                // TitleBar背景色
                .titleBgColor(Color.parseColor("#ffd100"))
                // 裁剪大小。needCrop为true的时候配置
                .cropSize(1, 1, 400, 400)
                .needCrop(true)
                // 第一个是否显示相机，默认true
                .needCamera(true)
                // 最大选择图片数量，默认9
                .maxNum(9)
                .build();

        // 跳转到图片选择器
        ISNav.getInstance().toListActivity(this, config, 1005);
    }

    private String localUri;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1005:
                if (data != null) {
                    if (data != null) {
                        List<String> pathList = data.getStringArrayListExtra("result");
                        localUri = pathList.get(0);
                        Glide.with(mContext)
                                .load(localUri)
                                .into(reportIV);
                    }
                }
                break;
        }
    }

    @Override
    public void ossResult(ArrayList<OssImageResponse> response) {
        picture = response.get(0).getFull_url();
        addInform();
    }

    @Override
    public void ossVideoResult(ArrayList<OssVideoResponse> response) {

    }

    private String picture = "";
    private void addInform() {
        String note = reportContentET.getText().toString();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hobbies.size(); i++) {
            sb.append(hobbies.get(i));
            if (i != (hobbies.size() - 1)){
                sb.append(",");
            }
        }
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("be_user_id", mBeUserID)
                    .put("reason_id", sb.toString())
                    .put("picture", picture)
                    .put("note", note)
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/inform/addInform", body, new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                NToast.shortToast(mContext, errInfo);
            }
            @Override
            public void onSuccess(String result) {
                NToast.shortToast(mContext, "举报成功");
                finish();
            }
        });
    }

    private ReasonListResponse response;
    private void getReasonList(){
        OKHttpUtils.getInstance().getRequest("app/inform/getReasonList", "", new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                NToast.shortToast(mContext, errInfo);
            }
            @Override
            public void onSuccess(String result) {
                try {
                    response = JsonMananger.jsonToBean(result, ReasonListResponse.class);
                    reportAdapter.setCards(response);
                    reportAdapter.notifyDataSetChanged();
                } catch (HttpException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
